<?php
/**
 * -----------------------------------------------------
 *
 * models Attribute_model
 *
 * @package     models
 * @subpackage  
 * @category    Attribute_model  -  产品属性模型类
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class Attribute_model extends CI_Model
{    
	public static $error;
	const TABLE = 'xc_pro_attribute'; // 表名

	public function __construct()
	{
		parent::__construct();
        $this->load->database();
	}

	/**
	 * 增加数据
	 */
	public function insert($data)
	{
		if($this->db->insert(self::TABLE,$data)){
			return true;
		}else{
			return false;
		}
	}

             
	/**
	 * 查询所有的数据，并且分页
	 */
	public function get_all_data_and_page($limit,$pid=null)
	{
		$where = '';
		if($pid){
			$where = 'where a.pro_id = '.$pid.' and attr_status = 1';
		}

		$sql = 'select a.*,b.pro_name
		from '.self::TABLE.' as a left join xc_products as b
		on a.pro_id=b.pro_id 
		'.$where.'
		order by a.attr_sort desc '.$limit;


		$query=$this->db->query($sql);
		if($query->num_rows()){
			return $query->result_array();
		}
	}

	/**
	 * 查询共有多少数据
	 */
	public function get_num_rows($pid=null)
	{
		if($pid){
			$this->db->where('pro_id',$pid);
		}
		$query = $this->db->get(self::TABLE);
		return $query->num_rows();
	}

	/**
	 * 编辑数据信息
	 */
	public function update($data,$id)
	{
		if($this->db->update(self::TABLE,$data,'attr_id = '.$id)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 根据id查询数据信息
	 */
	public function get_a_data_by_aid($aid)
	{
		$this->db->where('attr_id',$aid);
		$this->db->select('xc_pro_attribute.*,xc_products.pro_name');
		$this->db->join('xc_products', 'xc_pro_attribute.pro_id = xc_products.pro_id');
		$query = $this->db->get(self::TABLE);
		return $query->row_array();
	}

	/**
	 * 根据id删除数据
	 */
	public function delete($id)
	{
		$this->db->where('attr_id',$id);
		if($this->db->delete(self::TABLE)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * [get_date_by_pid 根据产品id获取属性]
	 * @param  [type] $pid [description]
	 * @return [type]      [description]
	 */
	public function get_date_by_pid($pid,$limit){
		$sql = 'select a.*,b.pro_name
		from '.self::TABLE.' as a left join xc_products as b
		on a.pro_id=b.pro_id 
		where a.attr_status = 1 and a.pro_id = '.$pid.' 
		order by a.attr_sort asc '.$limit;

		$query=$this->db->query($sql);
		if($query->num_rows()){
			return $query->result_array();
		}
	}

	
	public function get_attr($pid){
		$attr_arr = $this->get_product_attr($pid);
		foreach ($attr_arr as $key => $val) {
			if($val['attr_region'] == 1 ){ // 选择地区
				//$attr_arr[$key]['reg'] = $this->get_pro_region($pid);
				/*$arr = $this->get_pro_region($pid);
				foreach ($arr as $key => $value) {
					$this->xc_delivery_region($value);
				}*/
				$region_arr = $this->get_pro_region($pid);
				foreach ($region_arr as $k => $v) {
					$region_arr[$k]['regname'] = $this->get_region_name_by_id($v['reg_pid']).'-'.$this->get_region_name_by_id($v['reg_cid']).'-'.$this->get_region_name_by_id($v['reg_did']);
				}

				$attr_arr[$key]['reg'] = $region_arr;
			}

			if($val['attr_region'] == 0 ){ // 其他
				$attr_arr[$key]['reg'] = $this->get_pro_attribute_value($val['attr_id']);
			}
		}
		return $attr_arr;
	}

	public function get_product_attr($pid){
		$this->db->where('pro_id',$pid);
		$query = $this->db->get(self::TABLE);
		return $query->result_array();
	}

	public function get_pro_region($pid){
		$this->db->where('pro_id',$pid);
		$query = $this->db->get('xc_pro_region');
		return $query->result_array();
	}

	public function get_pro_attribute_value($attr_id){
		$this->db->where('attr_id',$attr_id);
		$query = $this->db->get('xc_pro_attribute_value');
		return $query->result_array();
	}


	public function get_region_all(){
		$query = $this->db->get('xc_delivery_region');
		return $query->result_array();
	}


	public function get_region_name_by_id($id){
	    $sql = "select * from xc_delivery_region where id=".$id;
        $query = $this->db->query($sql);
        if($query->num_rows() > 0){
            $arr = $query->row_array();
            return $arr['name'];
        }	
	}


}